package com.itheima.travel.service.impl;

import com.itheima.travel.dao.UserMapper;
import com.itheima.travel.pojo.User;
import com.itheima.travel.service.UserService;
import com.itheima.travel.utils.Md5Util;
import com.itheima.travel.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl implements UserService {
    /**
     * 用户注册
     * @param user
     * @return
     */
    public boolean register(User user) {
        //1.获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取dao接口的实现类对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //3.调用方法执行
        // 需要处理也逻辑: 对密码加密,在数据库中保存的密文
        // MD5
        // 123 ---> jkladjfuireuivfuihuwgheruirhjdvfd
        user.setPassword(Md5Util.getMD5(user.getPassword()));
        int i = mapper.register(user);
        //4.关闭sqlSession并返回结果
        MybatisUtils.closeSession(sqlSession);
        return i>0;
    }

    /**
     * 登录
     * @param u
     * @return
     */
    public User login(User u) {
        //获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //获取dao接口的实现类对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用方法
        // 对密码加密
        u.setPassword(Md5Util.getMD5(u.getPassword()));
        User user = mapper.login(u);
        // 关闭sqlSession
        MybatisUtils.closeSession(sqlSession);
        return user;
    }
}
